package com.lvhx.springboot.base;

import com.lvhx.springboot.entity.User;
import com.lvhx.springboot.mapper.extend.UserExtendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 加载聚合的入口类
 * 一般以property的形式注入业务逻辑的实现类，调用load方法进行聚合的加载
 */
@Repository("repositoryUser")
public class AccountAggregateUserRepository extends AbstractRepository<AccountAggregateUser> {

    @Autowired
    private UserExtendMapper userExtendMapper;

    public AccountAggregateUserRepository(){
        super(AccountAggregateUser.class);
    }
    @Override
    protected void doSave(AccountAggregateUser accountAggregateUser) {
        accountAggregateUser.persist();
    }
    @Override
    protected AccountAggregateUser doLoad(Object aggregateIdentifier, Long expectedVersion, boolean lockYn) {

        User user = null;

        if (lockYn){
            user = userExtendMapper.selectByPrimaryKeyWithLock((Long) aggregateIdentifier);
        }else {
            user = userExtendMapper.selectByPrimaryKey((Long) aggregateIdentifier);
        }

        if (user == null){
            throw new RuntimeException("所加载user不存在！");
        }

        AccountAggregateUser result = new AccountAggregateUser(user,lockYn,userExtendMapper);

        result.markInitialized();

        return result;
    }


}
